//
//  RootViewController.m
//  Jiexi
//
//  Created by lanou3g on 15-4-6.
//  Copyright (c) 2015年 ncy. All rights reserved.
//

#import "RootViewController.h"
#import "NSObject+ResolveData.h"
#import "Model.h"
#import "RootView.h"
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define DEGREES_TO_RADIANS(d) (d * M_PI / 180)

#define kRandomColor arc4random() % 256 / 255.0
@interface RootViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@property(nonatomic,strong)RootView *rc;
@end

@implementation RootViewController
- (void)loadView
{
//    //动态图
    self.rc = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.rc;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //webView
    /*
//    self.WebView = [[UIWebView alloc]initWithFrame:CGRectMake(10,10,self.view.frame.size.width, 100)];
//    
//    
//    self.WebView.delegate = self;
    NSString *string = [NSString stringWithFormat:@"http://star.zhuoyouapp.com/v1/contentServer/getContentById?contentId=455DE02A36ED4AB589CE1050E7CC73EA"];
    //    NSURL *url = [NSURL URLWithString:string];
    //    NSData *data = [NSData dataWithContentsOfURL:url];
    [NSObject resolveWithUrlStr:string method:@"GET" postBody:nil myBlock:^(id obj) {
        
        NSDictionary *dict = (NSDictionary *)obj;
    
        Model *model = [[Model alloc]init];
        
        
     
//        NSString * path = [[NSBundle mainBundle] pathForResource:@"html" ofType:@"txt"];
//        
//        NSMutableString * str = [NSMutableString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//        
//        NSString * cssPath = [[NSBundle mainBundle] pathForResource:@"htmlCSS" ofType:@"css"];
//        
//        NSRange rangeOfCSSPath = [str rangeOfString:@"#CSSPath#"];
//        //    NSLog(@"%@",NSStringFromRange(rangeOfCSSPath));
//        [str replaceCharactersInRange:rangeOfCSSPath withString:cssPath];
//        NSRange sourceRange = [str rangeOfString:@"#body#"];
//        if (model.content)
//        {
//            [str replaceCharactersInRange:sourceRange withString:model.content];
//        }
//        [self.WebView loadHTMLString:str baseURL:nil];
//        [self.view addSubview:self.WebView];
        
    }];
     
     */

    
    //self.view = _rc;
    //[self initLayer];
    
    
     button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
    
    //摇一摇
    [[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];

    
    
}

- (void)initLayer
{
    ballLayer=[CALayer layer];
    ballLayer.bounds = CGRectMake(0, 0, 106,IS_IPHONE_5?365+88:365);
    ballLayer.position = CGPointMake(160, IS_IPHONE_5?416+88:416);
    if (IS_IPHONE_5) {
        ballLayer.contents = (id)[UIImage imageNamed:@"1.png"].CGImage;
    }else{
        ballLayer.contents = (id)[UIImage imageNamed:@"1.png"].CGImage;
    }
    ballLayer.anchorPoint = CGPointMake(0.5, 1.0);
    [self.view.layer addSublayer:ballLayer];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置默认参数
    angle =30.0;
    timeInter = 0.05;
}

- (void)button:(UIButton *)sender
{
    NSLog(@"你是猪吗");
    button.userInteractionEnabled = NO;
    
    
    //左右摇摆时间是定义的时间的2倍
    [NSTimer scheduledTimerWithTimeInterval:timeInter*2
                                     target:self
                                   selector:@selector(ballAnmation:)
                                   userInfo:nil
                                    repeats:YES];
}
//气球动画
-(void)ballAnmation:(NSTimer *)theTimer
{
    //设置左右摇摆
    angle=-angle;
    if (angle > 0) {
        angle--;
    }else{
        angle++;
    }
    CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:(DEGREES_TO_RADIANS(angle))];
    rotationAnimation.duration = timeInter;
    rotationAnimation.autoreverses = YES; // Very convenient CA feature for an animation like this
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [ballLayer addAnimation:rotationAnimation forKey:@"revItUpAnimation"];
    if (angle == 0) {
        [theTimer invalidate];
        //动画完毕操作
        button.userInteractionEnabled = YES;
        

        angle =30.0;
        timeInter = 0.05;
    }
}




#pragma mark -----摇一摇
//开始摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self button:button];
}
//结束摇一摇
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"晃动结束");
    if (event.subtype == UIEventSubtypeMotionShake) {
        [self text];
        
    }
    
}
//取消摇一摇
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"晃动取消");
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}
- (void)text
{
    NSLog(@"晃动后触发事件");
    
    self.view.backgroundColor = [UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
